package beuthTaskList;

import java.util.GregorianCalendar;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.util.List;
import java.util.ArrayList;
import java.text.SimpleDateFormat;

import android.os.Environment;

public class TaskIO {

	private static FileInputStream fileInputStream; 
	private static FileOutputStream fileOutputStream; 
	private String pathToExternalStorage = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
	private String fileName = "test.txt";
	private String dataSeperator = "##";
	private SimpleDateFormat simpleFormat = new SimpleDateFormat ("d M y");

	public void save(List<Task> Tasks) {
        BufferedWriter out = null;
		try{
			fileOutputStream = new FileOutputStream(pathToExternalStorage + fileName);
			out = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
			for (Task task : Tasks) {
				out.write(task.getTimeSpend() + dataSeperator);
				out.write(task.getName() + dataSeperator);
				out.write(task.getStatusPercent() + dataSeperator);
				out.write(simpleFormat.format(task.getClosingDate().getTime()) + dataSeperator);
				out.write(task.getTaskType() + dataSeperator);
				out.write(task.getTaskPriority() + dataSeperator);
				out.write("\n");
			}
		} catch(IOException e){
			e.printStackTrace();
		}
		finally {
			try{
				if(out!=null) out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}  
    }


	public List<Task> readAllTask() {
		List<Task> foundTasks = new ArrayList<Task>();

	    BufferedReader in = null;
		try {
			fileInputStream = new FileInputStream(pathToExternalStorage	+ fileName);
			in = new BufferedReader(new InputStreamReader(fileInputStream));
	
			String line;
			GregorianCalendar date;
			while ((line = in.readLine()) != null) {
				date = new GregorianCalendar();
				Task tempTask = new Task();
				
				tempTask.setTimeSpend(Integer.parseInt(line.split(dataSeperator)[0]));
				tempTask.setName(line.split(dataSeperator)[1]);
				tempTask.setStatusPercent(Integer.parseInt(line.split(dataSeperator)[2]));
				try {
					date.setTime(simpleFormat.parse(line.split(dataSeperator)[3]));
				} catch (ParseException e) {
					e.printStackTrace();
				}					tempTask.setClosingDate(date);
				tempTask.setTaskType(TaskType.getFromString(line.split(dataSeperator)[4]));
				tempTask.setTaskPriority(Priority.getFromString(line.split(dataSeperator)[5]));
	
				foundTasks.add(tempTask);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return foundTasks;
	}

	//Start of user code protectedIOClassCodeTask
	//End of user code
} 
